﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NUnit.Framework;
using CondominioG1.Persistencia.NHibernate;
using CondominioG1.Dominio;
using CondominioG1.Negocio;

using Spring.Context;
using Spring.Context.Support;

namespace CondominioG1.Pruebas.TestService
{
    [TestFixture]
    public class TestResidenteService
    { 

        private IApplicationContext Spring;

        [TestFixtureSetUp]
        public void InicializarPruebas()
        {
            Spring = new XmlApplicationContext(
                "assembly://CondominioG1/CondominioG1/capaPersistencia.xml",
                "assembly://CondominioG1/CondominioG1/capaNegocio.xml"
                );

            // Obtengo una referencia a la clase de negocio a probar
            ReservaService reservaService = (ReservaService)Spring.GetObject("reservaService");
            ViviendaService viviendaService = (ViviendaService)Spring.GetObject("viviendaService");
            ResidenteService residenteService = (ResidenteService)Spring.GetObject("residenteService");
            CuotaService cuotaService = (CuotaService)Spring.GetObject("cuotaService");
            //Limpia la base de datos de Condominio
            reservaService.EliminarReservas();
            cuotaService.EliminaCuotas();
            viviendaService.EliminarViviendas();
            residenteService.EliminarResidentes();
            //Residente para modificar y eliminar usados en las pruebas
            residenteService.RegistrarResidente("44491253", "Ivanoska", "Conde", 31, "ivaconde@upc.edu.pe", "tastingo", "PROPIETARIO");
            residenteService.RegistrarResidente("71255612", "Logan", "Castillo", 31, "lcastillo@claro.com", "logan", "INQUILINO");
            residenteService.RegistrarResidente("40405050", "Silvia", "Delgado", 31, "sdelgado@upc.edu.pe", "tastingo", "PROPIETARIO");
        }

        [Test]
        public void Test_RegistrarResidente_correctamente()
        {
            ResidenteService ResidenteService = (ResidenteService)Spring.GetObject("residenteService");
            Residente residenteCreado = null;
            Assert.DoesNotThrow(delegate {
                residenteCreado = ResidenteService.RegistrarResidente("40714325", "Carlos", "Castillo", 31, "u900176@upc.edu.pe", "tastingo", "PROPIETARIO");
            });
            Assert.NotNull(residenteCreado);
            Assert.Greater(residenteCreado.Edad, 0);
        }

        [Test]
        public void Test_ModificarResidente_correctamente()
        {
            ResidenteService ResidenteService = (ResidenteService)Spring.GetObject("residenteService");
            Residente ResidenteModificado = null;
            Assert.DoesNotThrow(delegate {
                ResidenteModificado = ResidenteService.ModificarResidente("44491253", "Ivanoska", "CondeRios", 31, "ivaconde@bcp.com.pe", "tastingo", "PROPIETARIO");
            });
            Assert.NotNull(ResidenteModificado);
        }

        [Test]
        public void Test_EliminarResidente_correctamente()
        {
            ResidenteService ResidenteService = (ResidenteService)Spring.GetObject("residenteService");
            Residente ResidenteEliminado = null;
            Assert.DoesNotThrow(delegate {
            ResidenteEliminado = ResidenteService.EliminarResidente("71255612");
            });
        }


        [Test]
        public void Test_RegistrarResidente_incorrectamente()
        {
            ResidenteService ResidenteService = (ResidenteService)Spring.GetObject("residenteService");
            Residente residenteCreado = null;
            // Pruebas solicitadas
            Assert.Catch<Exception>(delegate
            {
                residenteCreado = ResidenteService.RegistrarResidente("40405050", "Silvia", "Delgado", 31, "sdelgado@upc.edu.pe", "tastingo", "PROPIETARIO");
            });
        }
    }
}